//
//  AppDelegate.swift
//  我的微博
//
//  Created by teacher on 15/7/27.
//  Copyright © 2015年 itheima. All rights reserved.
//

import UIKit


/// 在类的外面写的常量或者变量就是全局能够访问的
/// 视图控制器切换通知字符串
let HMRootViewControllerSwitchNotification = "HMRootViewControllerSwitchNotification"

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

       NSNotificationCenter.defaultCenter().addObserver(self, selector: "notification", name: HMRootViewControllerSwitchNotification, object: nil)
        
        setupAppearance()
        
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.backgroundColor = UIColor.whiteColor()
        window?.rootViewController = defauleVC()
        
        window?.makeKeyAndVisible()
        
        return true
    }
    func   notification(no:NSNotification) {
        
        let isHome = no.object as! Bool
        
        window?.rootViewController = isHome ? HomeTableViewController() : WelcomeVCViewController()
    }
    
    func defauleVC() -> UIViewController {
        if UserAccount.loadUserAccounts() == nil {
        
            return MainViewController()
        }
        else {
           let verSion =  Double(NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String)!
            
          let normalVerdion = NSUserDefaults.standardUserDefaults().doubleForKey("normalVerdion")
            
            if verSion > normalVerdion {
                
                
                NSUserDefaults.standardUserDefaults().setDouble(verSion, forKey: "normalVerdion")
                return NewFeatureCollectionVC()
            }
            return  WelcomeVCViewController()
        
        }
    
        
    }
    
    /// 设置外观
    private func setupAppearance() {
        // 一经设置，全局有效，应该尽早设置
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        UITabBar.appearance().tintColor = UIColor.orangeColor()
    }
}

